package com.flyee.controller.tools.cartoon{
	
	import com.flyee.controller.tools.cartoon.model.AppearanceConfig;
	import com.flyee.controller.tools.cartoon.model.As3Cartoon;
	import com.flyee.controller.tools.cartoon.model.CartoonData;
	import com.flyee.controller.tools.cartoon.ui.changeequip.control.TransformCartoonDataManager;
	import com.flyee.controller.tools.cartoon.ui.changeequip.model.TransformCartoonDataModel;
	import com.flyee.service.CartoonFlexService;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.ClassLoader;
	import com.flyee.utils.CutCharacter;
	import com.flyee.utils.MyLog;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	
	public class EquipManager{
	
		private var loadList:Array = new Array();
		private var returnFun:Function = null;
		private var returnId : String = null;
		private var appearanceList:Array = null;
		private var isOpen:Boolean = true;
		private var changeID:String = null;
		public var timer:Timer = new Timer(1000);
		/**静态默认图板*/
		private var staticArray : Array;
		
		private var loadedAppearanceDic:Dictionary = new Dictionary();
		private var equipObj:Object = null;
		
		private var registFunList:Array = new Array();
		
		public function EquipManager(){
//			timer.addEventListener(TimerEvent.TIMER, timerHandle);
		}
		public function registLisener(fun:Function):void
		{
			registFunList.push(fun);
		}
		private function dispthEquip():void
		{
			var tempLength:uint = registFunList.length;
			for(var i:int = 0; i < tempLength ; i++)
			{
				var tempFun:Function = registFunList[i];
				if(tempFun != null)
				{
					var tempArray:Array = new Array();
					this.getStaticArray(tempArray);
					tempFun(tempArray);
				}
			}
		}
		public function removeLisener(fun:Function):void
		{
			var tempLength:uint = registFunList.length;
			for(var i:int = 0; i < tempLength ; i++)
			{
				if(registFunList[i] == fun)
				{
					registFunList.splice(i,1);
				}
			}
		}
		public function getEquip(userID:String,appearanceArray:Array,returnFun:Function,changeID:String = null) : void {
			var tempObj:Object = {user:userID,changeID:changeID,array:appearanceArray,retrunFun:returnFun}
			loadList.push(tempObj);
			loadEquip();
			
		}
		private function loadEquip() : void{
			if(!isOpen){
				return
			}			
			if(this.loadList.length > 0){
				this.isOpen = false;
				var tempObj:Object = this.loadList.shift();
				if(tempObj.changeID == null)
				{
					tempObj.changeID = ObjectCollection.DEFAULT_USER_ID;
				}
				if(tempObj.user && tempObj.array){
					this.returnFun = tempObj.retrunFun;
					this.returnId = tempObj.user;
					this.changeID = tempObj.changeID;
					equipObj = tempObj;
					this.staticArray = this.loadedAppearanceDic[tempObj.changeID]
					if(this.staticArray != null)
					{
						this.getStaticArray(tempObj.array);
						if(this.returnFun != null)
						{
							this.returnFun(this.returnId);
						}
						if(this.returnId == ObjectCollection.master.id)
						{
							this.dispthEquip();
						}
						this.appearanceList = null;
						this.returnFun = null;
						this.changeID = null;
						this.isOpen = true;
						this.staticArray = null;
						this.equipObj = null;
						this.loadEquip();
					}else{
						this.appearanceList = null;
						this.appearanceList = new Array();
						
						var tempServer:CartoonFlexService = new CartoonFlexService();
						
						//tempServer.getCartoonByUser(tempObj.changeID,this.getCartoon);
						this.loadDepot(tempObj.changeID)
					}
				}else{
					//this.loadList.unshift(tempObj);
					this.isOpen = true;
					loadEquip();
				}
			}
		}
		private function loadDepot(equipID:String):void
		{
			var loaderURL:String = null;
			if(equipID == ObjectCollection.DEFAULT_USER_ID)
			{
				loaderURL = "resource/Appearance_zi.swf";
			}
			else
			{
				loaderURL = "resource/Appearance_bai.swf";
			}
			var loader:ClassLoader = new ClassLoader();
			loader.loadByURL(loaderURL);
			loader.addEventListener(Event.COMPLETE,depotCompleteHandle);
			loader.addEventListener(IOErrorEvent.IO_ERROR,depotErrorHandle);
		}
		private function depotCompleteHandle(e:Event):void
		{
			var temp:ClassLoader = e.currentTarget as ClassLoader;
			this.getArrayByImage(temp)
		}
		private function depotErrorHandle(e:IOErrorEvent):void
		{
			MyLog.debug("error depot by Appearance");
		}
		private function getArrayByImage(loader:ClassLoader):void
		{
			var tempEquipArray:Array = new Array();
			var tempArray:Array = null;
			var tempClass:Class = null;

			tempClass = loader.GetClass("stop",loader.loadinfo);
			tempArray = CutCharacter.cut(new tempClass(),110,200);
			tempEquipArray[AppearanceConfig.MOVE_BY_STAND] = tempArray;
				
			tempClass = loader.GetClass("move",loader.loadinfo);
			tempArray = CutCharacter.cut(new tempClass(),110,200);
			tempEquipArray[AppearanceConfig.MOVE_BY_WALK] = tempArray;
				
			tempClass = loader.GetClass("ready",loader.loadinfo);
			tempArray = CutCharacter.cut(new tempClass(),110,200);
			tempEquipArray[AppearanceConfig.MOVE_BY_READY] = tempArray;
				
			tempClass = loader.GetClass("attack",loader.loadinfo);
			tempArray = CutCharacter.cut(new tempClass(),110,200);
			tempEquipArray[AppearanceConfig.MOVE_BY_FIGHT] = tempArray;
				
			tempClass = loader.GetClass("magice",loader.loadinfo);
			tempArray = CutCharacter.cut(new tempClass(),110,200);
			tempEquipArray[AppearanceConfig.MOVE_BY_MAGICE] = tempArray;
			
			this.loadedAppearanceDic[this.changeID] = tempEquipArray;
			this.staticArray = tempEquipArray;
			this.getStaticArray(this.equipObj.array);
			
			loader.clear();
			if(this.returnFun != null)
			{
				this.returnFun(this.returnId);
			}
			
			if(this.returnId == ObjectCollection.master.id)
			{
				this.dispthEquip();
			}
			
			this.appearanceList = null;
			this.returnFun = null;
			this.changeID = null;
			this.equipObj = null;
			this.isOpen = true;
			this.loadEquip();
		}
		private function getCartoon(t:As3Cartoon):void{
			if(t == null){
					
			}else{
				var tempServer:CartoonFlexService = new CartoonFlexService();
				tempServer.listByCartoonId(t.id,this.getCartoonData);
			}
		}
		
		private function getCartoonData(data:ArrayCollection):void{
			var tempArray:Array = data.source;
			var tempLength:uint = tempArray.length;
			for(var i:int = 0; i < tempLength; i++){
				var tempCartoonData:CartoonData = tempArray[i];
				
				TransformCartoonDataManager.getInstance().regist(tempCartoonData,this.getBitmapdata);
			}
		}
		
		private function getStaticArray(tmp:Array) : Array {
			tmp.splice(0);
			//var tmp : Array = new Array();
			trace("-------------------------" + new Date().time);
			for(var i : int = 0 ; i < staticArray.length ; i++){
				if(staticArray[i] != null){
					var tmp1 : Array = new Array();
					for(var j : int = 0 ; j < staticArray[i].length ; j++){
						if(staticArray[i][j] != null){
							var tmp2 : Array = new Array();
							for(var k : int = 0 ; k < staticArray[i][j].length ; k++){
								if(staticArray[i][j][k] != null){
									var imageData : Bitmap = staticArray[i][j][k] as Bitmap;
									var imageData1 : Bitmap = new Bitmap(imageData.bitmapData);
									tmp2.push(imageData1);
								}								
							}
							tmp1.push(tmp2);
						}
					}
					tmp.push(tmp1);
				}else{
					tmp.push(null);
				}
			}
			trace("-------------------------" + new Date().time);
			return tmp;
		}
		private function getArrayCopy(tmp : Array) : Array {
//			var tmp : Array = new Array();
			var returnArray:Array = new Array();

			for(var i : int = 0 ; i < tmp.length ; i++){
				if(tmp[i] != null){
					var tmp1 : Array = new Array();
					for(var j : int = 0 ; j < tmp[i].length ; j++){
						if(tmp[i][j] != null){
							var tmp2 : Array = new Array();
							for(var k : int = 0 ; k < tmp[i][j].length ; k++){
								if(tmp[i][j][k] != null){
									var imageData : Bitmap = tmp[i][j][k] as Bitmap;
									var imageData1 : Bitmap = new Bitmap(imageData.bitmapData);
									tmp2.push(imageData1);
								}								
							}
							tmp1.push(tmp2);
						}
					}
					returnArray.push(tmp1);
				}else{
					returnArray.push(null);
				}
			}
			return returnArray;
		}
		private var count :uint = 0;
		public function getBitmapdata(model:TransformCartoonDataModel):void{
			if(model){
				var typeArray:Array = null;
				if(this.appearanceList[model.type] == null){
					typeArray = new Array();
					this.appearanceList[model.type] = typeArray;
				}else{
					typeArray = this.appearanceList[model.type];
				}
				var dirArray:Array = null;
				if(typeArray[model.dir] == null){
					dirArray = new Array();
					typeArray[model.dir] = dirArray;
				}else{
					dirArray = typeArray[model.dir];
				}
				dirArray[model.frame] = new Bitmap(model.bitmapdata.clone());
			}else{
				MyLog.debug("error by create png")
			}
			if(TransformCartoonDataManager.getInstance().getListLength() == 0){
				this.loadedAppearanceDic[this.changeID] = appearanceList;
				this.staticArray = appearanceList;
				this.getStaticArray(this.equipObj.array);
				
				if(this.returnFun != null){
					this.returnFun(this.returnId);
				}
				//this.staticArray = this.getArrayCopy(appearanceList);
//				var tempUIc:UIComponent = new UIComponent();
//				tempUIc.x = count*300
//				ObjectCollection.uiContainer.addChild(tempUIc);
//				var tempRule:CartoonPlayRule = new CartoonPlayRule(appearanceList,tempUIc)
//				tempRule.autoPlay = true;
				this.appearanceList = null;
				this.returnFun = null;
				this.changeID = null;
				this.equipObj = null;
				this.isOpen = true;
				this.loadEquip();
				
			}
		}
		
//		private function timerHandle(e:TimerEvent):void{
//			trace(TransformCartoonDataManager.getInstance().getListLength());
//		}
	}
}